﻿#!/usr/bin/env python
# -*- coding: utf-8 -*-

# resources.py

# Copyright 2011 Diego Hansen Hahn (aka DiegoHH) <diegohh90 [at] hotmail [dot] com>

# lazynds is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.

# lazynds is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with lazynds. If not, see <http://www.gnu.org/licenses/>.

from xml.dom import minidom

def create_xml_resource(rc):
    fd = open(rc, 'w')

    doc = minidom.Document()
    mainNode = doc.createElement('lazynds')
    doc.appendChild(mainNode)

    doc.writexml(fd, '', '', '', 'UTF-8')
    fd.close()

def update_xml_resource(rc, lazy_list):
    doc = minidom.parse(rc)
    e = doc.firstChild

    for x in lazy_list:
        elem = doc.createElement("data")
        elem.setAttribute("offset", str(x[0]))
        if isinstance(x[1], int):
             elem.setAttribute("bios_flag", str(x[1]))
        else:
             elem.setAttribute("plugin_prefix", x[1])

        e.appendChild(elem)

    fd = open(rc, 'w')
    doc.writexml(fd, '', '\t', '\n', 'UTF-8')
    fd.close()
    return

def parse_xml_resource(rc, lazy_list):
    doc = minidom.parse(rc)
    for c in doc.firstChild.childNodes:
        if c.nodeType == c.ELEMENT_NODE:
            offset = int(c.getAttribute("offset"))
            if c.hasAttribute("bios_flag"):
                flag = int(c.getAttribute("bios_flag"))
            elif c.hasAttribute("plugin_prefix"):
                flag = c.getAttribute("plugin_prefix")
            lazy_list.append((offset, flag))
    return



